import {Component, Input, OnInit} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {NzModalRef, NzModalService} from 'ng-zorro-antd/modal';
import {NzMessageService} from 'ng-zorro-antd/message';

@Component({
  selector: 'app-user-label-add',
  templateUrl: './user-label-add.component.html',
  styleUrls: ['./user-label-add.component.scss']
})
export class UserLabelAddComponent implements OnInit {
  @Input() userId: any;
  category = [];
  tageContent = [];
  formData = {
    tagClassify: '',
    tagContent: '',
    customContent: '',
    remark: '',
  };

  constructor(
    private httpClient: HttpClient,
    private nzModalRef: NzModalRef,
    private nzMessageService: NzMessageService,
  ) {
  }

  ngOnInit(): void {
    this.httpClient.get('/dict/detail', {
      params: {
        pageNum: '1',
        pageSize: '9999',
        parentKey: 'user_tags_classify'
      }
    }).subscribe((res: any) => {
      if (res.code === 0) {
        this.category = res.data.list.records;
      }
    });
    this.httpClient.get('/dict/detail', {
      params: {
        pageNum: '1',
        pageSize: '9999',
        parentKey: 'user_tags_preset'
      }
    }).subscribe((res: any) => {
      if (res.code === 0) {
        this.tageContent = res.data.list.records;
      }
    });
  }

  cancel() {
    this.nzModalRef.destroy();
  }

  save() {
    if (!this.userId) {
      this.nzMessageService.error('没有检测到用户id');
      return;
    }
    if (!this.formData.tagContent && !this.formData.customContent) {
      this.nzMessageService.error('请选择标签内容或输入自定义内容');
      return;
    }
    this.httpClient.post('/userTag/update', {
      userId: this.userId,
      tagClassify: this.formData.tagClassify,
      tagContent: this.formData.tagContent || this.formData.customContent,
      remark: this.formData.remark,
    }).subscribe((res: any) => {
      if (res.code === 0) {
        this.nzModalRef.destroy({
          refresh: true
        });
      } else {
        this.nzMessageService.error(res.message);
      }
    });
  }
}
